/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package javaapplication6;

/**
 *
 * @author 彭福德
 */
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class MenuDemo implements ItemListener,ActionListener{
    JFrame frame = new JFrame("Menu Demo");
    JTextField tf = new JTextField();
    
    public static void main(String args[]){
        MenuDemo menuDemo = new MenuDemo();
        menuDemo.go();


    }
    
    public void go(){
        JMenuBar menubar = new JMenuBar();
        frame.setJMenuBar(menubar);

        JMenu menu,submenu;
        JMenuItem menuItem;
        menu = new JMenu("File");
        menu.setMnemonic(KeyEvent.VK_F);
        menubar.add(menu);

        menuItem = new JMenuItem("Open...");
        menuItem.setMnemonic(KeyEvent.VK_0);
        menuItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_1,ActionEvent.ALT_MASK));
        menuItem.addActionListener(this);
        menu.add(menuItem);

        menuItem = new JMenuItem("Save",KeyEvent.VK_S);
        menuItem.addActionListener(this);
        menuItem.setEnabled(false);
        menu.add(menuItem);

        menuItem = new JMenuItem("Close");
        menuItem.setMnemonic(KeyEvent.VK_C);
        menuItem.addActionListener(this);
        menu.add(menuItem);



        Container cp = frame.getContentPane();
        cp.add(tf,BorderLayout.SOUTH);

        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setSize(300, 200);
        frame.setVisible(true);

    }

    public void itemStateChanged(ItemEvent e){
        int state = e.getStateChange();
        JMenuItem amenuItem = (JMenuItem)e.getSource();
        String command = amenuItem.getText();
        if(state == ItemEvent.SELECTED)
            tf.setText(command + "SELECTED");
        else
            tf.setText(command + "DESELECTED");

    }

    public void actionPerformed(ActionEvent e){
        tf.setText(e.getActionCommand());

        if(e.getActionCommand() == "Exit"){
            System.exit(0);
        }
    }


}
